<?php

namespace app\merchants\controller;

use app\common\validate\shop\ShopStockValid;
use app\merchants\loader\Services;
use think\Exception;

/**
 * 门店库存管理
 */
class ShopStock extends ApiBase
{

    /**
     * 门店库存列表
     * @return mixed
     */
    public function shop_page()
    {
        $data = input('get.');
        try {
            $where[] = ['mer_id', '=', $this->adminUser['mer_id']];
            if (!empty($data['shop_id'])) {
                $where[] = ['shop_id', '=', $data['shop_id']];
            }
            if (!empty($this->adminUser['sub_admin_ids'])) {
                $admin_ids = $this->adminUser['sub_admin_ids'];
                $where[] = ['shop_id', 'in', function ($query) use ($admin_ids) {
                    $query->name('shop')->where('op_user', 'in', $admin_ids)->field('id');
                }];
            }
            $where[] = ['product_code' , 'in' , function($query){
                $query->name('product')->where(['mer_id' => $this->adminUser['mer_id'], 'delete_at' => null])->field("code");
            }];
            $reault = Services::ShopStock()->page($where, $data['page'] ?? 1, $data['page_size'] ?? 30);
            api_result(API_SUCCESS, '请求成功', $reault);
        }catch (Exception $e){
            api_result($e);
        }
    }


    /**
     * 门店商品库存列表
     */
    public function shop_page_info(): void
    {
        try {
            $data = input('get.');

            $where[] = ['a.mer_id', '=', $this->adminUser['mer_id']];

            if (!empty($data['warn_flag'])) {
                $where[] = ['warn_flag', '=', $data['warn_flag']];

                if ((int)$data['warn_flag'] === 1) {
                    $where_num = 'a.sum_num<=b.warn_num';
                    //$where[] = ['b.warn_num','=' , 1];
                }
            }
            if (!empty($this->adminUser['sub_admin_ids'])) {
                $admin_ids = $this->adminUser['sub_admin_ids'];
                $where[] = ['shop_id', 'in', function ($query) use ($admin_ids) {
                    $query->name('shop')->where('op_user', 'in', $admin_ids)->field('id');
                }];
            }
            if (!empty($data['shop_id'])) {
                $where[] = ['shop_id', '=', $data['shop_id']];
            }

            if (!empty($data['product_code'])) {
                $where[] = ['product_code', '=', $data['product_code']];
            }
            $option = ['where' => $where, 'where_num' => $where_num ?? null];

            $reault = Services::ShopStock()->shopPage($option, $this->adminUser['mer_id'],$data['shop_id'] ?: 0, $data['warn_flag'] ?? 0, $data['page'] ?? 1, $data['page_size'] ?? 30);

            api_result(API_SUCCESS, '请求成功', $reault);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 门店库存详情
     */
    public function shop_info(): void
    {
        try {
            $data = input('get.');
            validate(ShopStockValid::class)->scene('shop_info')->check($data);
            $reault = Services::ShopStock()->shopInfo($data['id']);
            api_result(API_SUCCESS, '请求成功', $reault);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 预警状态修改
     */
    public function shop_save_warn(): void
    {
        try {
            $data = input('post.');
            validate(ShopStockValid::class)->scene('shop_save_warn')->check($data);
            Services::ShopStock()->shopSaveWarn($data['id'], $data['type']);
            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 门店库存调拨
     */
    public function shop_save_stock(): void
    {
        try {
            $data = input('post.');
            validate(ShopStockValid::class)->scene('shop_save_stock')->check($data);
            Services::ShopStock()->shopSaveStock(
                $data['id'],
                (int)$data['num'],
                (int)$data['type'],
                $this->adminUser['admin_id'],
                (int)($data['shop_id'] ?? 0)
            );
            api_result(API_SUCCESS, '请求成功', $data['id']);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 商品库存分页列表
     */
    public function shop_product_stock(): void
    {
        try {
            $data = input('get.');

            $mer_id = $this->adminUser['mer_id'];
            $where = [];

            if (!empty($data['keyword'])) {
                $where[] = ['product_code', '=', $data['keyword']];
            }

            $res = Services::ShopStock()->shopProductStock($mer_id, $where, $data['page'] ?? 1, $data['page_size'] ?? 30);
            api_result(API_SUCCESS, '请求成功', $res);
        } catch (Exception $e) {
            api_result($e);
        }
    }

}
